{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<i>Copyright (c) Microsoft Corporation. All rights reserved.</i>\n",
    "\n",
    "<i>Licensed under the MIT License.</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sequential Recommender Quick Start\n",
    "\n",
    "### Example: SLi_Rec : Adaptive User Modeling with Long and Short-Term Preferences for Personailzed Recommendation\n",
    "Unlike a general recommender such as Matrix Factorization or xDeepFM (in the repo) which doesn't consider the order of the user's activities, sequential recommender systems take the sequence of the user behaviors as context and the goal is to predict the items that the user will interact in a short time (in an extreme case, the item that the user will interact next).\n",
    "\n",
    "This notebook aims to give you a quick example of how to train a sequential model based on a public Amazon dataset. Currently, we can support NextItNet \\[4\\], GRU4Rec \\[2\\], Caser \\[3\\], A2SVD \\[1\\] and SLi_Rec \\[1\\]. Without loss of generality, this notebook takes [SLi_Rec model](https://www.microsoft.com/en-us/research/uploads/prod/2019/07/IJCAI19-ready_v1.pdf) for example.\n",
    "SLi_Rec \\[1\\] is a deep learning-based model aims at capturing both long and short-term user preferences for precise recommender systems. To summarize, SLi_Rec has the following key properties:\n",
    "\n",
    "* It adopts the attentive \"Asymmetric-SVD\" paradigm for long-term modeling;\n",
    "* It takes both time irregularity and semantic irregularity into consideration by modifying the gating logic in LSTM.\n",
    "* It uses an attention mechanism to dynamic fuse the long-term component and short-term component.\n",
    "\n",
    "In this notebook, we test SLi_Rec on a subset of the public dataset: [Amazon_reviews](http://snap.stanford.edu/data/amazon/productGraph/categoryFiles/reviews_Movies_and_TV_5.json.gz) and [Amazon_metadata](http://snap.stanford.edu/data/amazon/productGraph/categoryFiles/meta_Movies_and_TV.json.gz)\n",
    "\n",
    "This notebook is well tested under TF 1.15.0. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0. Global Settings and Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "System version: 3.6.11 | packaged by conda-forge | (default, Aug  5 2020, 20:09:42) \n",
      "[GCC 7.5.0]\n",
      "Tensorflow version: 1.15.2\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "sys.path.append(\"../../\")\n",
    "import os\n",
    "import logging\n",
    "import papermill as pm\n",
    "import scrapbook as sb\n",
    "from tempfile import TemporaryDirectory\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "tf.get_logger().setLevel('ERROR') # only show error messages\n",
    "\n",
    "from reco_utils.common.timer import Timer\n",
    "from reco_utils.common.constants import SEED\n",
    "from reco_utils.recommender.deeprec.deeprec_utils import (\n",
    "    prepare_hparams\n",
    ")\n",
    "from reco_utils.dataset.amazon_reviews import download_and_extract, data_preprocessing\n",
    "from reco_utils.dataset.download_utils import maybe_download\n",
    "\n",
    "\n",
    "from reco_utils.recommender.deeprec.models.sequential.sli_rec import SLI_RECModel as SeqModel\n",
    "####  to use the other model, use one of the following lines:\n",
    "# from reco_utils.recommender.deeprec.models.sequential.asvd import A2SVDModel as SeqModel\n",
    "# from reco_utils.recommender.deeprec.models.sequential.caser import CaserModel as SeqModel\n",
    "# from reco_utils.recommender.deeprec.models.sequential.gru4rec import GRU4RecModel as SeqModel\n",
    "\n",
    "#from reco_utils.recommender.deeprec.models.sequential.nextitnet import NextItNetModel\n",
    "\n",
    "from reco_utils.recommender.deeprec.io.sequential_iterator import SequentialIterator\n",
    "#from reco_utils.recommender.deeprec.io.nextitnet_iterator import NextItNetIterator\n",
    "\n",
    "print(\"System version: {}\".format(sys.version))\n",
    "print(\"Tensorflow version: {}\".format(tf.__version__))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "##  ATTENTION: change to the corresponding config file, e.g., caser.yaml for CaserModel \n",
    "yaml_file = '../../reco_utils/recommender/deeprec/config/sli_rec.yaml'  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": [
     "parameters"
    ]
   },
   "outputs": [],
   "source": [
    "EPOCHS = 10\n",
    "BATCH_SIZE = 400\n",
    "RANDOM_SEED = SEED  # Set None for non-deterministic result\n",
    "\n",
    "data_path = os.path.join(\"..\", \"..\", \"tests\", \"resources\", \"deeprec\", \"slirec\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  1. Input data format\n",
    "The input data contains 8 columns, i.e.,   `<label> <user_id> <item_id> <category_id> <timestamp> <history_item_ids> <history_cateory_ids> <hitory_timestamp>`  columns are seperated by `\"\\t\"`.  item_id and category_id denote the target item and category, which means that for this instance, we want to guess whether user user_id will interact with item_id at timestamp. `<history_*>` columns record the user behavior list up to `<timestamp>`, elements are separated by commas.  `<label>` is a binary value with 1 for positive instances and 0 for negative instances.  One example for an instance is: \n",
    "\n",
    "`1       A1QQ86H5M2LVW2  B0059XTU1S      Movies  1377561600      B002ZG97WE,B004IK30PA,B000BNX3AU,B0017ANB08,B005LAIHW2  Movies,Movies,Movies,Movies,Movies   1304294400,1304812800,1315785600,1316304000,1356998400` \n",
    "\n",
    "In data preprocessing stage, we have a script to generate some ID mapping dictionaries, so user_id, item_id and category_id will be mapped into interager index starting from 1. And you need to tell the input iterator where is the ID mapping files are. (For example, in the next section, we have some mapping files like user_vocab, item_vocab, and cate_vocab).  The data preprocessing script is at [reco_utils/dataset/amazon_reviews.py](../../reco_utils/dataset/amazon_reviews.py), you need to call the `_create_vocab(train_file, user_vocab, item_vocab, cate_vocab)` function. Note that ID vocabulary only creates from the train_file, so the new IDs in valid_file or test_file will be regarded as unknown IDs and assigned with a defualt 0 index.\n",
    "\n",
    "Only the SLi_Rec model is time-aware. For the other models, you can just pad some meaningless timestamp in the data files to fill up the format, the models will ignore these columns.\n",
    "\n",
    "We use Softmax to the loss function. In training and evalution stage, we group 1 positive instance with num_ngs negative instances. Pair-wise ranking can be regarded as a special case of Softmax ranking, where num_ngs is set to 1. \n",
    "\n",
    "More specifically,  for training and evalation, you need to organize the data file such that each one positive instance is followd by num_ngs negative instances. Our program will take 1+num_ngs lines as a unit for Softmax calculation. num_ngs is a parameter you need to pass to the `prepare_hparams`, `fit` and `run_eval` function. `train_num_ngs` in `prepare_hparams` denotes the number of negative instances for training, where a recommended number is 4. `valid_num_ngs` and `num_ngs` in `fit` and `run_eval` denote the number in evalution. In evaluation, the model calculates metrics among the 1+num_ngs instances. For the `predict` function, since we only need to calcuate a socre for each individual instance, there is no need for num_ngs setting.  More details and examples will be provided in the following sections.\n",
    "\n",
    "For training stage, if you don't want to prepare negative instances, you can just provide positive instances and set the parameter `need_sample=True, train_num_ngs=train_num_ngs` for function `prepare_hparams`, our model will dynamicly sample `train_num_ngs` instances as negative samples in each mini batch.\n",
    "\n",
    "###  Amazon dataset\n",
    "Now let's start with a public dataset containing product reviews and metadata from Amazon, which is widely used as a benchmark dataset in recommemdation systems field."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "\n",
    "# for test\n",
    "train_file = os.path.join(data_path, r'train_data')\n",
    "valid_file = os.path.join(data_path, r'valid_data')\n",
    "test_file = os.path.join(data_path, r'test_data')\n",
    "user_vocab = os.path.join(data_path, r'user_vocab.pkl')\n",
    "item_vocab = os.path.join(data_path, r'item_vocab.pkl')\n",
    "cate_vocab = os.path.join(data_path, r'category_vocab.pkl')\n",
    "output_file = os.path.join(data_path, r'output.txt')\n",
    "\n",
    "reviews_name = 'reviews_Movies_and_TV_5.json'\n",
    "meta_name = 'meta_Movies_and_TV.json'\n",
    "reviews_file = os.path.join(data_path, reviews_name)\n",
    "meta_file = os.path.join(data_path, meta_name)\n",
    "train_num_ngs = 4 # number of negative instances with a positive instance for training\n",
    "valid_num_ngs = 4 # number of negative instances with a positive instance for validation\n",
    "test_num_ngs = 9 # number of negative instances with a positive instance for testing\n",
    "sample_rate = 0.01 # sample a small item set for training and testing here for fast example\n",
    "\n",
    "input_files = [reviews_file, meta_file, train_file, valid_file, test_file, user_vocab, item_vocab, cate_vocab]\n",
    "\n",
    "if not os.path.exists(train_file):\n",
    "    download_and_extract(reviews_name, reviews_file)\n",
    "    download_and_extract(meta_name, meta_file)\n",
    "    data_preprocessing(*input_files, sample_rate=sample_rate, valid_num_ngs=valid_num_ngs, test_num_ngs=test_num_ngs)\n",
    "    #### uncomment this for the NextItNet model, because it does not need to unfold the user history\n",
    "    # data_preprocessing(*input_files, sample_rate=sample_rate, valid_num_ngs=valid_num_ngs, test_num_ngs=test_num_ngs, is_history_expanding=False)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.1 Prepare hyper-parameters\n",
    "prepare_hparams() will create a full set of hyper-parameters for model training, such as learning rate, feature number, and dropout ratio. We can put those parameters in a yaml file (a complete list of parameters can be found under our config folder) , or pass parameters as the function's parameters (which will overwrite yaml settings).\n",
    "\n",
    "Parameters hints: <br>\n",
    "`need_sample` controls whether to perform dynamic negative sampling in mini-batch. \n",
    "`train_num_ngs` indicates how many negative instances followed by one positive instances.  <br>\n",
    "Examples: <br>\n",
    "(1) `need_sample=True and train_num_ngs=4`:  There are only positive instances in your training file. Our model will dynamically sample 4 negative instances for each positive instances in mini-batch. Note that if need_sample is set to True, train_num_ngs should be greater than zero. <br>\n",
    "(2) `need_sample=False and train_num_ngs=4`: In your training file, each one positive line is followed by 4 negative lines. Note that if need_sample is set to False, you must provide a traiing file with negative instances, and train_num_ngs should match the number of negative number in your training file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "### NOTE:  \n",
    "### remember to use `_create_vocab(train_file, user_vocab, item_vocab, cate_vocab)` to generate the user_vocab, item_vocab and cate_vocab files, if you are using your own dataset rather than using our demo Amazon dataset.\n",
    "hparams = prepare_hparams(yaml_file, \n",
    "                          embed_l2=0., \n",
    "                          layer_l2=0., \n",
    "                          learning_rate=0.001,  # set to 0.01 if batch normalization is disable\n",
    "                          epochs=EPOCHS,\n",
    "                          batch_size=BATCH_SIZE,\n",
    "                          show_step=20,\n",
    "                          MODEL_DIR=os.path.join(data_path, \"model/\"),\n",
    "                          SUMMARIES_DIR=os.path.join(data_path, \"summary/\"),\n",
    "                          user_vocab=user_vocab,\n",
    "                          item_vocab=item_vocab,\n",
    "                          cate_vocab=cate_vocab,\n",
    "                          need_sample=True,\n",
    "                          train_num_ngs=train_num_ngs, # provides the number of negative instances for each positive instance for loss computation.\n",
    "            )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2 Create data loader\n",
    "Designate a data iterator for the model. All our sequential models use SequentialIterator. \n",
    "data format is introduced aboved. \n",
    "\n",
    "<br>Validation and testing data are files after negative sampling offline with the number of `<num_ngs>` and `<test_num_ngs>`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_creator = SequentialIterator\n",
    "#### uncomment this for the NextItNet model, because it needs a special data iterator for training\n",
    "#input_creator = NextItNetIterator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Create model\n",
    "When both hyper-parameters and data iterator are ready, we can create a model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model = SeqModel(hparams, input_creator, seed=RANDOM_SEED)\n",
    "\n",
    "## sometimes we don't want to train a model from scratch\n",
    "## then we can load a pre-trained model like this: \n",
    "#model.load_model(r'your_model_path')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's see what is the model's performance at this point (without starting training):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'auc': 0.4857, 'logloss': 0.6931, 'mean_mrr': 0.2665, 'ndcg@2': 0.1357, 'ndcg@4': 0.2186, 'ndcg@6': 0.2905, 'group_auc': 0.4849}\n"
     ]
    }
   ],
   "source": [
    "# test_num_ngs is the number of negative lines after each positive line in your test_file\n",
    "print(model.run_eval(test_file, num_ngs=test_num_ngs)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "AUC=0.5 is a state of random guess. We can see that before training, the model behaves like random guessing.\n",
    "\n",
    "#### 2.1 Train model\n",
    "Next we want to train the model on a training set, and check the performance on a validation dataset. Training the model is as simple as a function call:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step 20 , total_loss: 1.6078, data_loss: 1.6078\n",
      "step 40 , total_loss: 1.6054, data_loss: 1.6054\n",
      "eval valid at epoch 1: auc:0.4975,logloss:0.6929,mean_mrr:0.4592,ndcg@2:0.3292,ndcg@4:0.5125,ndcg@6:0.5915,group_auc:0.4994\n",
      "step 20 , total_loss: 1.5786, data_loss: 1.5786\n",
      "step 40 , total_loss: 1.4193, data_loss: 1.4193\n",
      "eval valid at epoch 2: auc:0.6486,logloss:0.6946,mean_mrr:0.5567,ndcg@2:0.472,ndcg@4:0.6292,ndcg@6:0.6669,group_auc:0.6363\n",
      "step 20 , total_loss: 1.3229, data_loss: 1.3229\n",
      "step 40 , total_loss: 1.3079, data_loss: 1.3079\n",
      "eval valid at epoch 3: auc:0.6887,logloss:0.8454,mean_mrr:0.6032,ndcg@2:0.537,ndcg@4:0.6705,ndcg@6:0.7022,group_auc:0.683\n",
      "step 20 , total_loss: 1.3521, data_loss: 1.3521\n",
      "step 40 , total_loss: 1.2250, data_loss: 1.2250\n",
      "eval valid at epoch 4: auc:0.6978,logloss:0.7005,mean_mrr:0.6236,ndcg@2:0.5622,ndcg@4:0.6881,ndcg@6:0.7175,group_auc:0.699\n",
      "step 20 , total_loss: 1.2826, data_loss: 1.2826\n",
      "step 40 , total_loss: 1.2795, data_loss: 1.2795\n",
      "eval valid at epoch 5: auc:0.7152,logloss:0.6695,mean_mrr:0.6382,ndcg@2:0.582,ndcg@4:0.7009,ndcg@6:0.7286,group_auc:0.7139\n",
      "step 20 , total_loss: 1.2214, data_loss: 1.2214\n",
      "step 40 , total_loss: 1.2521, data_loss: 1.2521\n",
      "eval valid at epoch 6: auc:0.722,logloss:0.6141,mean_mrr:0.637,ndcg@2:0.5796,ndcg@4:0.6993,ndcg@6:0.7276,group_auc:0.7116\n",
      "step 20 , total_loss: 1.1884, data_loss: 1.1884\n",
      "step 40 , total_loss: 1.1957, data_loss: 1.1957\n",
      "eval valid at epoch 7: auc:0.7287,logloss:0.6183,mean_mrr:0.6417,ndcg@2:0.5875,ndcg@4:0.7031,ndcg@6:0.7312,group_auc:0.7167\n",
      "step 20 , total_loss: 1.1779, data_loss: 1.1779\n",
      "step 40 , total_loss: 1.1616, data_loss: 1.1616\n",
      "eval valid at epoch 8: auc:0.7342,logloss:0.6584,mean_mrr:0.6538,ndcg@2:0.6006,ndcg@4:0.7121,ndcg@6:0.7402,group_auc:0.7248\n",
      "step 20 , total_loss: 1.1299, data_loss: 1.1299\n",
      "step 40 , total_loss: 1.2055, data_loss: 1.2055\n",
      "eval valid at epoch 9: auc:0.7324,logloss:0.6268,mean_mrr:0.6541,ndcg@2:0.5981,ndcg@4:0.7129,ndcg@6:0.7404,group_auc:0.7239\n",
      "step 20 , total_loss: 1.1927, data_loss: 1.1927\n",
      "step 40 , total_loss: 1.1909, data_loss: 1.1909\n",
      "eval valid at epoch 10: auc:0.7369,logloss:0.6122,mean_mrr:0.6611,ndcg@2:0.6087,ndcg@4:0.7181,ndcg@6:0.7457,group_auc:0.731\n",
      "[(1, {'auc': 0.4975, 'logloss': 0.6929, 'mean_mrr': 0.4592, 'ndcg@2': 0.3292, 'ndcg@4': 0.5125, 'ndcg@6': 0.5915, 'group_auc': 0.4994}), (2, {'auc': 0.6486, 'logloss': 0.6946, 'mean_mrr': 0.5567, 'ndcg@2': 0.472, 'ndcg@4': 0.6292, 'ndcg@6': 0.6669, 'group_auc': 0.6363}), (3, {'auc': 0.6887, 'logloss': 0.8454, 'mean_mrr': 0.6032, 'ndcg@2': 0.537, 'ndcg@4': 0.6705, 'ndcg@6': 0.7022, 'group_auc': 0.683}), (4, {'auc': 0.6978, 'logloss': 0.7005, 'mean_mrr': 0.6236, 'ndcg@2': 0.5622, 'ndcg@4': 0.6881, 'ndcg@6': 0.7175, 'group_auc': 0.699}), (5, {'auc': 0.7152, 'logloss': 0.6695, 'mean_mrr': 0.6382, 'ndcg@2': 0.582, 'ndcg@4': 0.7009, 'ndcg@6': 0.7286, 'group_auc': 0.7139}), (6, {'auc': 0.722, 'logloss': 0.6141, 'mean_mrr': 0.637, 'ndcg@2': 0.5796, 'ndcg@4': 0.6993, 'ndcg@6': 0.7276, 'group_auc': 0.7116}), (7, {'auc': 0.7287, 'logloss': 0.6183, 'mean_mrr': 0.6417, 'ndcg@2': 0.5875, 'ndcg@4': 0.7031, 'ndcg@6': 0.7312, 'group_auc': 0.7167}), (8, {'auc': 0.7342, 'logloss': 0.6584, 'mean_mrr': 0.6538, 'ndcg@2': 0.6006, 'ndcg@4': 0.7121, 'ndcg@6': 0.7402, 'group_auc': 0.7248}), (9, {'auc': 0.7324, 'logloss': 0.6268, 'mean_mrr': 0.6541, 'ndcg@2': 0.5981, 'ndcg@4': 0.7129, 'ndcg@6': 0.7404, 'group_auc': 0.7239}), (10, {'auc': 0.7369, 'logloss': 0.6122, 'mean_mrr': 0.6611, 'ndcg@2': 0.6087, 'ndcg@4': 0.7181, 'ndcg@6': 0.7457, 'group_auc': 0.731})]\n",
      "best epoch: 10\n",
      "Time cost for training is 3.22 mins\n"
     ]
    }
   ],
   "source": [
    "with Timer() as train_time:\n",
    "    model = model.fit(train_file, valid_file, valid_num_ngs=valid_num_ngs) \n",
    "\n",
    "# valid_num_ngs is the number of negative lines after each positive line in your valid_file \n",
    "# we will evaluate the performance of model on valid_file every epoch\n",
    "print('Time cost for training is {0:.2f} mins'.format(train_time.interval/60.0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.2  Evaluate model\n",
    "\n",
    "Again, let's see what is the model's performance now (after training):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'auc': 0.7174, 'logloss': 0.6149, 'mean_mrr': 0.4835, 'ndcg@2': 0.3939, 'ndcg@4': 0.4982, 'ndcg@6': 0.5503, 'group_auc': 0.7073}\n"
     ]
    }
   ],
   "source": [
    "res_syn = model.run_eval(test_file, num_ngs=test_num_ngs)\n",
    "print(res_syn)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sb.glue(\"res_syn\", res_syn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we want to get the full prediction scores rather than evaluation metrics, we can do this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = model.predict(test_file, output_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The data was downloaded in tmpdir folder. You can delete them manually if you do not need them any more."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.3  Running models with large dataset\n",
    "Here are performances using the whole amazon dataset among popular sequential models with 1,697,533 positive instances.\n",
    "<br>Settings for reproducing the results:\n",
    "<br>`learning_rate=0.001, dropout=0.3, item_embedding_dim=32, cate_embedding_dim=8, l2_norm=0, batch_size=400, \n",
    "train_num_ngs=4, valid_num_ngs=4, test_num_ngs=49`\n",
    "\n",
    "\n",
    "We compare the running time with CPU only and with GPU on the larger dataset. It appears that GPU can significantly accelerate the training. Hardware specification for running the large dataset: \n",
    "<br>GPU: Tesla P100-PCIE-16GB\n",
    "<br>CPU: 6 cores Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz\n",
    " \n",
    "| Models | AUC | g-AUC | NDCG@2 | NDCG@10 | seconds per epoch on GPU | seconds per epoch on CPU| config |\n",
    "| :------| :------: | :------: | :------: | :------: | :------: | :------: | :------ |\n",
    "| A2SVD | 0.8251 | 0.8178 | 0.2922 | 0.4264 | 249.5 | 440.0 | N/A |\n",
    "| GRU4Rec | 0.8411 | 0.8332 | 0.3213 | 0.4547 | 439.0 | 4285.0 | max_seq_length=50, hidden_size=40|\n",
    "| Caser | 0.8244 | 0.8171 | 0.283 | 0.4194 | 314.3 | 5369.9 | T=1, n_v=128, n_h=128, L=3, min_seq_length=5|\n",
    "| SLi_Rec | 0.8631 | 0.8519 | 0.3491 | 0.4842 | 549.6 | 5014.0 | attention_size=40, max_seq_length=50, hidden_size=40|\n",
    "| NextItNet* | 0.6793 | 0.6769 | 0.0602 | 0.1733 | 112.0 | 214.5 | min_seq_length=3, dilations=\\[1,2,4,1,2,4\\], kernel_size=3 |\n",
    "\n",
    " Note 1: The five models are grid searched with a coarse granularity and the results are for reference only.\n",
    " <br>Note 2: NextItNet model requires a dataset with strong sequence property, but the Amazon dataset used in this notebook does not meet that requirement, so NextItNet Model may not performance good. If you wish to use other datasets with strong sequence property, NextItNet is recommended.\n",
    " <br>Note 3: Time cost of NextItNet Model is significantly shorter than other models because it doesn't need a history expanding of training data."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Online serving\n",
    "In this section, we provide a simple example to illustrate how we can use the trained model to serve for production demand.\n",
    "\n",
    "Suppose we are in a new session. First let's load a previous trained model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading saved model in ../../tests/resources/deeprec/slirec/model/best_model\n",
      "INFO:tensorflow:Restoring parameters from ../../tests/resources/deeprec/slirec/model/best_model\n"
     ]
    }
   ],
   "source": [
    "model_best_trained = SeqModel(hparams, input_creator, seed=RANDOM_SEED)\n",
    "path_best_trained = os.path.join(hparams.MODEL_DIR, \"best_model\")\n",
    "print('loading saved model in {0}'.format(path_best_trained))\n",
    "model_best_trained.load_model(path_best_trained)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's see if we load the model correctly. The testing metrics should be close to the numbers we have in the training stage."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'auc': 0.7249,\n",
       " 'logloss': 0.5924,\n",
       " 'mean_mrr': 0.4946,\n",
       " 'ndcg@2': 0.4075,\n",
       " 'ndcg@4': 0.5107,\n",
       " 'ndcg@6': 0.5607,\n",
       " 'group_auc': 0.7133}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_best_trained.run_eval(test_file, num_ngs=test_num_ngs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And we make predictions using this model. In the next step, we will make predictions using a serving model. Then we can check if the two result files are consistent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<reco_utils.recommender.deeprec.models.sequential.sli_rec.SLI_RECModel at 0x7f2da0326e80>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_best_trained.predict(test_file, output_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exciting. Now let's start our quick journey of online serving. \n",
    "\n",
    "For efficient and flexible serving, usually we only keep the necessary computation nodes and froze the TF model to a single pb file, so that we can easily compute scores with this unified pb file in both Python or Java:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "with model_best_trained.sess as sess:\n",
    "    graph_def = model_best_trained.graph.as_graph_def()\n",
    "    output_graph_def = tf.graph_util.convert_variables_to_constants(\n",
    "        sess,\n",
    "        graph_def,\n",
    "        [\"pred\"]\n",
    "    )\n",
    "\n",
    "    outfilepath = os.path.join(hparams.MODEL_DIR, \"serving_model.pb\")\n",
    "    with tf.gfile.GFile(outfilepath, 'wb') as f:\n",
    "        f.write(output_graph_def.SerializeToString())\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The serving logic is as simple as feeding the feature values to the corresponding input nodes, and fetch the score from the output node. \n",
    "\n",
    "In our model, input nodes are some placeholders and control variables (such as is_training, layer_keeps). We can get the nodes by their name:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "class LoadFrozedPredModel:\n",
    "    def __init__(self, graph):\n",
    "        self.pred = graph.get_tensor_by_name('import/pred:0') \n",
    "        self.items = graph.get_tensor_by_name('import/items:0') \n",
    "        self.cates = graph.get_tensor_by_name('import/cates:0') \n",
    "        self.item_history = graph.get_tensor_by_name('import/item_history:0') \n",
    "        self.item_cate_history = graph.get_tensor_by_name('import/item_cate_history:0') \n",
    "        self.mask = graph.get_tensor_by_name('import/mask:0')  \n",
    "        self.time_from_first_action = graph.get_tensor_by_name('import/time_from_first_action:0') \n",
    "        self.time_to_now = graph.get_tensor_by_name('import/time_to_now:0') \n",
    "        self.layer_keeps = graph.get_tensor_by_name('import/layer_keeps:0') \n",
    "        self.is_training = graph.get_tensor_by_name('import/is_training:0') \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def infer_as_serving(model, infile, outfile, hparams, iterator, sess):\n",
    "    preds = []\n",
    "    \n",
    "    for batch_data_input in iterator.load_data_from_file(infile, batch_num_ngs=0):\n",
    "        if batch_data_input:\n",
    "            feed_dict = {\n",
    "                model.layer_keeps:np.ones(3, dtype=np.float32),\n",
    "                model.is_training:False,\n",
    "                model.items: batch_data_input[iterator.items],\n",
    "                model.cates: batch_data_input[iterator.cates],\n",
    "                model.item_history: batch_data_input[iterator.item_history],\n",
    "                model.item_cate_history: batch_data_input[iterator.item_cate_history],\n",
    "                model.mask: batch_data_input[iterator.mask],\n",
    "                model.time_from_first_action: batch_data_input[iterator.time_from_first_action],\n",
    "                model.time_to_now: batch_data_input[iterator.time_to_now]\n",
    "            }\n",
    "            step_pred = sess.run(model.pred, feed_dict=feed_dict)\n",
    "            preds.extend(np.reshape(step_pred, -1))\n",
    "                \n",
    "    with open(outfile, \"w\") as wt:\n",
    "        for line in preds:\n",
    "            wt.write('{0}\\n'.format(line))\n",
    "            "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is the main pipeline for inferring in an online serving manner. You can compare the 'output_serving.txt' with 'output.txt' to see if the results are consistent.\n",
    "\n",
    "The input file format is the same as introduced in Section 1 'Input data format'. In serving stage, since we do not need a groundtrue lable, so for the label column, you can simply place any number like a zero. The iterator will parse the input file and convert into the required format for model's feed_dictionary. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "G = tf.Graph()\n",
    "with tf.gfile.GFile(\n",
    "        os.path.join(hparams.MODEL_DIR, \"serving_model.pb\"),\n",
    "        'rb'\n",
    ") as f, G.as_default():\n",
    "    graph_def_optimized = tf.GraphDef()\n",
    "    graph_def_optimized.ParseFromString(f.read())\n",
    "    \n",
    "    ####  uncomment this line if you want to check what conent is included in the graph\n",
    "    #print('graph_def_optimized = ' + str(graph_def_optimized))\n",
    "\n",
    "\n",
    "with tf.Session(graph=G) as sess:\n",
    "    tf.import_graph_def(graph_def_optimized)\n",
    "\n",
    "    model = LoadFrozedPredModel(sess.graph)\n",
    "    \n",
    "    serving_output_file = os.path.join(data_path, r'output_serving.txt')  \n",
    "    iterator = input_creator(hparams, tf.Graph())\n",
    "    infer_as_serving(model, test_file, serving_output_file, hparams, iterator, sess)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reference\n",
    "\\[1\\] Zeping Yu, Jianxun Lian, Ahmad Mahmoody, Gongshen Liu, Xing Xie. Adaptive User Modeling with Long and Short-Term Preferences for Personailzed Recommendation. In Proceedings of the 28th International Joint Conferences on Artificial Intelligence, IJCAI’19, Pages 4213-4219. AAAI Press, 2019.\n",
    "\n",
    "\\[2\\] Balázs Hidasi, Alexandros Karatzoglou, Linas Baltrunas, Domonkos Tikk. Session-based Recommendations with Recurrent Neural Networks. ICLR (Poster) 2016\n",
    "\n",
    "\\[3\\] Tang, Jiaxi, and Ke Wang. Personalized top-n sequential recommendation via convolutional sequence embedding. Proceedings of the Eleventh ACM International Conference on Web Search and Data Mining. ACM, 2018.\n",
    "\n",
    "\\[4\\] Yuan, F., Karatzoglou, A., Arapakis, I., Jose, J. M., & He, X. A Simple Convolutional Generative Network for Next Item Recommendation. WSDM, 2019"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python (reco_gpu)",
   "language": "python",
   "name": "reco_gpu"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
